<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/11/14
 * Time: 17:08
 * Created by Created by Panxsoft.
 */

namespace App\Models\Options;

use App\Models\Language;
use App\Models\Products\ProductOptionValue;
use App\Services\MobileApp\AppEnv;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Option
 *
 * @property-read Collection $optionValueDescriptions
 * @property-read int $option_id
 * @property-read OptionDescription $optionDescription*
 * @property string type
 * @property Collection productOptionValues
 * @package App\Models\Options
 * @mixin Builder
 */
class Option extends Model
{
    const DEFAULT_OPTION_TYPE = 'radio'; // app端，默认返回单选的选项
    
    const TYPE_RATIO = 'radio';
    const TYPE_CHECKBOX = 'checkbox';
    const TYPE_SELECT = 'select';

    const TYPE_DATE = 'date';
    const TYPE_TIME = 'time';

    // 单选和多选的类型
    const SELECTION_TYPES = [
        self::TYPE_RATIO, self::TYPE_CHECKBOX, self::TYPE_SELECT,
    ];

    // 时间的类型
    const DATE_TIME_TYPES = [
        self::TYPE_DATE, self::TYPE_TIME
    ];

    protected $guarded = [];

    protected $table = 'option';
    protected $primaryKey = 'option_id';

    public function optionDescription()
    {
        return $this->hasOne(OptionDescription::class, 'option_id', 'option_id')
            ->where('language_id', AppEnv::languageId());
    }

    /**
     * @used-by $optionValueDescriptions
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function optionValueDescriptions()
    {
        return $this->hasMany(OptionValueDescription::class, 'option_id', 'option_id')
            ->where('language_id', AppEnv::languageId());
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function productOptionValues()
    {
        return $this->hasMany(ProductOptionValue::class, 'option_id', 'option_id');
    }

}